﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class EquipmentTag : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			UIApplication application = commandData.Application;
			Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
			Document document = application.ActiveUIDocument.Document;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			activeView = document.ActiveView;
			if (activeView is View3D && !(activeView as View3D).IsLocked)
			{
				YJKMessageBox.Warning("该三维视图无法标注，请锁定视角后再进行此操作！");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				transaction = new Transaction(document, "设备标注");
				transaction.Start();
				for (;;)
				{
					ISelectionFilter selectionFilter = new EquipmentFilter();
					Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择要标注的设备");
					Element element = document.GetElement(reference);
					XYZ pnt = selection.PickPoint("请选择标注位置的点");
					string text = "";
					BuiltInCategory integerValue = (Autodesk.Revit.DB.BuiltInCategory)(element.Category.Id.IntegerValue);
					BuiltInCategory builtInCategory = BuiltInCategory.OST_MechanicalEquipmentTags;
					Parameter parameter = element.GetParameter("统计名称");
                    if ((int)integerValue == -2001140)
					{
						if (parameter == null)
						{
							text = "HY_机械设备标记型号";
						}
						else
						{
							text = "HY_机械设备标记";
						}
						builtInCategory = BuiltInCategory.OST_MechanicalEquipmentTags;
					}
                    else if ((int)integerValue == -2001160)
					{
						text = "HY_卫浴装置标记";
						builtInCategory = BuiltInCategory.OST_PlumbingFixtureTags;
					}
					YJKRevitTools.DeleteTagOfElement(document, builtInCategory, element);
					FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(document, text, (Autodesk.Revit.DB.BuiltInCategory)(element.Category.Id.IntegerValue));
					Parameter parameter2 = (element as FamilyInstance).Symbol.GetParameter(BuiltInParameter.ALL_MODEL_MODEL);
					if (parameter2 != null && string.IsNullOrEmpty(parameter2.AsString()))
					{
						parameter2.Set((element as FamilyInstance).Name);
						document.Regenerate();
					}
					if (familySymbol != null)
					{
						IndependentTag independentTag = RevitVersionFuncs.NewTag(document, activeView, new Reference(element), false, 0, 0, pnt);
						independentTag.ChangeTypeId(new ElementId(familySymbol.Id.IntegerValue));
						independentTag.HasLeader = true;
					}
					else if (familySymbol == null)
					{
						string markFamilyPath = UnifiedModified.GetMarkFamilyPath(document, text);
						document.LoadFamily(markFamilyPath);
						RevitVersionFuncs.NewTag(document, activeView, new Reference(element), false, 0, 0, pnt).HasLeader = true;
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() == 1)
				{
					transaction.Commit();
				}
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
